package org.freespace.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.freespace.model.*;
import org.freespace.service.*;
import org.freespace.utils.ConsulUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class MonitorRegisterSerivceImpl implements IMonitorRegisterSerivce {

    @Autowired
    IEnvironmentMachineService environmentMachineService;

    @Autowired
    IJobMachineService jobMachineService;

    @Autowired
    IMachineService machineService;

    @Autowired
    IJobService jobService;

    @Autowired
    IJobEnvironmentService jobEnvironmentService;

    @Autowired
    ConsulUtil consulUtil;

    @Override
    public void stressMachineRegister(Integer jobId) {
        List<JobMachine> allLockMachines = jobMachineService.list(Wrappers.<JobMachine>lambdaQuery().eq(JobMachine::getJobId, jobId));
        if(CollectionUtil.isEmpty(allLockMachines)) {
            return;
        }

        allLockMachines.forEach(jobMachine -> {
            Machine machine = machineService.getById(jobMachine.getMachineId());
            consulUtil.registerStressMachineToConsul(machine.getIp(), jobId+"");
        });
    }

    @Override
    public void appServerRegister(Integer jobId) {
        Job envId = jobService.getById(jobId);
        LambdaQueryWrapper<EnvironmentMachine> wrapper = Wrappers.<EnvironmentMachine>lambdaQuery().eq(EnvironmentMachine::getEnvId, envId);
        List<EnvironmentMachine> environmentMachineList = environmentMachineService.list(wrapper);

        //备份环境信息到job环境表
        List<JobEnvironment> jobEnvironmentList = new ArrayList<>();
        for (EnvironmentMachine environmentMachine : environmentMachineList) {
            JobEnvironment jobEnvironment = new JobEnvironment();
            BeanUtils.copyProperties(environmentMachine, jobEnvironment);
            jobEnvironment.setJobId(jobId);
            jobEnvironmentList.add(jobEnvironment);
        }
        jobEnvironmentService.saveBatch(jobEnvironmentList);

        environmentMachineList.forEach(environmentMachine -> {
            consulUtil.registerAppServerToConsul(environmentMachine.getIp(), jobId+"");
        });
    }

    @Override
    public void jobRegister(Integer jobId) {
        stressMachineRegister(jobId);
        appServerRegister(jobId);
    }

    @Override
    public void stressMachineDeregister(Integer jobId) {
        List<JobMachine> allLockMachines = jobMachineService.list(Wrappers.<JobMachine>lambdaQuery().eq(JobMachine::getJobId, jobId));
        log.info("jobId:{} stressMachineDeregister.list:{}", jobId, allLockMachines);
        if(CollectionUtil.isEmpty(allLockMachines)) {
            return;
        }

        for (JobMachine jobMachine : allLockMachines) {
            Machine machine = machineService.getById(jobMachine.getMachineId());
            consulUtil.deregisterFromConsul(machine.getIp(), jobId+"");
        }
    }

    @Override
    public void appServerDeRegister(Integer jobId) {
        Job envId = jobService.getById(jobId);
        LambdaQueryWrapper<JobEnvironment> wrapper = Wrappers.<JobEnvironment>lambdaQuery().eq(JobEnvironment::getEnvId, envId);
        List<JobEnvironment> environmentMachineList = jobEnvironmentService.list(wrapper);
        log.info("jobId:{} appServerDeRegister.list:{}", jobId, environmentMachineList);
        environmentMachineList.forEach(environmentMachine -> {
            consulUtil.deregisterFromConsul(environmentMachine.getIp(), jobId+"");
        });
    }

    @Override
    public void jobDeregister(Integer jobId) {
        stressMachineDeregister(jobId);
        appServerDeRegister(jobId);
    }
}
